#!/bin/bash

set -euo pipefail

# Check if shfmt is installed
if ! command -v shfmt >/dev/null 2>&1; then
  # If it's not installed, for now skip autoformatting. We can make it a tool dependency
  # in the future, if we want.
  exit 0
fi

FILES=$(git diff --cached --name-only --diff-filter=ACMR)
[ -z "$FILES" ] && exit 0

# Format all selected files
echo "$FILES" | xargs shfmt --apply-ignore -w

# Add back the modified/formatted files to staging
echo "$FILES" | xargs git add

exit 0
